package cn.com.blueInfo.bpm.template.controller;

import cn.com.blueInfo.bpm.common.controller.BaseController;
import cn.com.blueInfo.bpm.common.validation.Create;
import cn.com.blueInfo.bpm.common.validation.Update;
import cn.com.blueInfo.bpm.common.validation.UpdatePartial;
import cn.com.blueInfo.bpm.template.request.WorkItemDTO;
import cn.com.blueInfo.bpm.template.response.WorkItemVO;
import cn.com.blueInfo.bpm.template.service.WorkItemService;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.groups.Default;
import java.util.List;

/**
 * 节点任务实例表 前端控制器
 * @author suxch
 * @since 2025-08-24 01:41:16
 */
@Tag(name = "节点任务实例 接口")
@RestController
@RequestMapping("/template/workItem")
public class WorkItemController extends BaseController {

    @Autowired
    private WorkItemService workItemService;

    /**
     * 新建节点任务实例
     * @param workItemDTO 节点任务实例参数
     * @return ResultInfo
     */
    @Operation(summary = "新增节点任务实例")
    @PostMapping("/addInfo")
    public ResultInfo<Integer> createWorkItem(
            @Validated({Create.class, Default.class}) @RequestBody WorkItemDTO workItemDTO) {
        return workItemService.addWorkItem(workItemDTO);
    }

    /**
     * 批量新建节点任务实例
     * @param workItemDTOList 节点任务实例参数列表
     * @return ResultInfo
     */
    @Operation(summary = "批量新建节点任务实例")
    @PostMapping("/addInfoList")
    public ResultInfo<Integer> batchCreateWorkItem(
            @Validated({Create.class, Default.class}) @RequestBody List<WorkItemDTO> workItemDTOList) {
        return workItemService.batchAddWorkItem(workItemDTOList);
    }

    /**
     * 根据ID删除节点任务实例
     * @param workItemId 节点任务实例ID
     * @return ResultInfo
     */
    @Operation(summary = "根据ID删除节点任务实例")
    @DeleteMapping("/removeInfo/{workItemId}")
    public ResultInfo<Integer> deleteWorkItemById(
            @Parameter(name = "ID") @PathVariable Long workItemId) {
        return workItemService.removeWorkItem(workItemId);
    }

    /**
     * 批量根据ID删除节点任务实例
     * @param workItemIdList 节点任务实例ID列表
     * @return ResultInfo
     */
    @Operation(summary = "批量根据ID删除节点任务实例")
    @DeleteMapping("/removeInfoList/{workItemIdList}")
    public ResultInfo<Integer> batchDeleteWorkItemById(
            @Parameter(name = "ID") @PathVariable List<Long> workItemIdList) {
        return workItemService.batchRemoveWorkItem(workItemIdList);
    }

    /**
     * 根据ID全量更新节点任务实例
     * @param workItemId 节点任务实例ID
     * @param workItemDTO 节点任务实例参数
     * @return ResultInfo
     */
    @Operation(summary = "根据ID全量更新节点任务实例")
    @PutMapping("/modifyInfo/{workItemId}")
    public ResultInfo<Integer> updateWorkItemFull(
            @Parameter(name = "ID") @PathVariable Long workItemId,
            @Validated({Update.class, Default.class}) @RequestBody WorkItemDTO workItemDTO) {
        validateUpdateId(workItemId, workItemDTO.getId());
        return workItemService.modifyWorkItem(workItemDTO);
    }

    /**
     * 根据ID部分更新节点任务实例
     * @param workItemId 节点任务实例ID
     * @param workItemUpdatePartialDTO 节点任务实例参数
     * @return ResultInfo
     */
    @Operation(summary = "根据ID部分更新节点任务实例")
    @PatchMapping("/modifyPartialInfo/{workItemId}")
    public ResultInfo<Integer> updateWorkItemPartial(
            @Parameter(name = "ID") @PathVariable Long workItemId,
            @Validated({UpdatePartial.class, Default.class}) @RequestBody
            WorkItemDTO.UpdatePartialDTO workItemUpdatePartialDTO) {
        validateUpdateId(workItemId, workItemUpdatePartialDTO.getId());
        return workItemService.modifyWorkItemPartial(workItemUpdatePartialDTO);
    }

    /**
     * 根据ID查询节点任务实例
     * @param workItemId 节点任务实例ID
     * @return ResultInfo
     */
    @Operation(summary = "根据ID查询节点任务实例")
    @GetMapping("/getInfo/{workItemId}")
    public ResultInfo<WorkItemVO> getWorkItemById(
            @Parameter(name = "ID") @PathVariable Long workItemId) {
        return workItemService.queryWorkItemById(workItemId);
    }

    /**
     * 分页查询节点任务实例
     * @param workItemQueryDTO 节点任务实例参数
     * @return ResultInfo
     */
    @Operation(summary = "分页查询节点任务实例")
    @GetMapping("/getInfos")
    public ResultInfo<Page<WorkItemVO>> getWorkItemPage(
            @Validated WorkItemDTO.QueryDTO workItemQueryDTO) {
        return workItemService.queryWorkItemPage(workItemQueryDTO);
    }

    /**
     * 根据条件查询节点任务实例
     * @param workItemQueryDTO 节点任务实例参数
     * @return ResultInfo
     */
    @Operation(summary = "根据条件查询节点任务实例")
    @GetMapping("/getInfoList")
    public ResultInfo<List<WorkItemVO>> getWorkItemList(
            @Validated WorkItemDTO.QueryDTO workItemQueryDTO) {
        return workItemService.queryWorkItemList(workItemQueryDTO);
    }

}
